//
//  CLPlayerItem.swift
//  clreader
//
//  Created by colin on 2019/3/28.
//  Copyright © 2019 colin. All rights reserved.
//

import AVKit

extension CLAudioPlayer {
    
    class CLPlayerItem: AVPlayerItem {
        
        let tlStatus = Observable(AVPlayerItem.Status.unknown)
        
        init?(urlString:String?) {
            if let urlString_ = urlString, let url = URL(string: urlString_) {
                let asset = AVURLAsset(url: url)
                self.init(asset: asset)
            } else {
                return nil
            }
            self.addObserver(self, forKeyPath: "status", options: .new, context: nil)
        }
        
        deinit {
            self.removeObserver(self, forKeyPath: "status")
        }
        
        override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
            self.tlStatus.value = self.status
        }
        
        override init(asset: AVAsset, automaticallyLoadedAssetKeys: [String]?) {
            super.init(asset: asset, automaticallyLoadedAssetKeys: automaticallyLoadedAssetKeys)
        }
        
    }
}
